import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
Vue.use(VueRouter)

import login from '@/views/login'
import layout from '@/views/layout/layout'
import dataSummary from '@/views/layout/mainarea/dataSummary'
import userList from '@/views/layout/mainarea/userList'
import questionBank from '@/views/layout/mainarea/questionBank'
import enterpriseList from '@/views/layout/mainarea/enterpriseList'
import courseList from '@/views/layout/mainarea/courseList'

let routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: login, meta: { title: "黑马面面" } },
    {
        path: '/layout', component: layout, redirect: "/dataSummary", children: [
            { path: '/dataSummary', component: dataSummary, meta: { title: "数据概览" } },
            { path: '/userList', component: userList, meta: { title: "用户列表" } },
            { path: '/questionBank', component: questionBank, meta: { title: "题库列表" } },
            { path: '/enterpriseList', component: enterpriseList, meta: { title: "企业列表" } },
            { path: '/courseList', component: courseList, meta: { title: "课程列表" } },
        ]
    }
]

let router = new VueRouter({ routes })

//路由前置守卫
router.beforeEach((to, from, next) => {
    NProgress.start()
    next()
    return (to, from)
})

// 路由后置守卫
router.afterEach((to, from) => {
    NProgress.done()
    // document.getElementById('title').innerText = to.meta.title;
    // document.title = to.meta.title;
    document.title = to.meta.title
    return (to, from)
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

export default router